package com.liaowei.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liaowei.common.BaseResponse;
import com.liaowei.model.domain.File;
import com.liaowei.model.vo.UserVO;
import com.liaowei.system.mapper.FileMapper;
import com.liaowei.system.model.vo.FileVO;
import com.liaowei.system.openfeign.UserClient;
import com.liaowei.system.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author LindaMan
* @description 针对表【wy_file(文件表)】的数据库操作Service实现
* @createDate 2023-05-06 16:29:39
*/
@Service
@RequiredArgsConstructor
public class FileServiceImpl extends ServiceImpl<FileMapper, File>
    implements FileService {

    private final FileMapper fileMapper;
    private final UserClient userClient;


    /**
     * 根据tagId查询资源列表
     * @param tagId 标签id
     * @return List<FileVO>
     */
    @Override
    public List<FileVO> searchFileListByTagId(Long tagId) {
        // 根据标签id查询资源列表
        List<File> fileList = fileMapper.selectList(Wrappers.lambdaQuery(File.class).eq(File::getTagId, tagId));

        // 封装VO
        List<FileVO> fileVOList = fileList.stream().map(f -> {
            FileVO vo = new FileVO();
            BeanUtil.copyProperties(f,vo);
            Long createBy = f.getCreateBy();
            BaseResponse<UserVO> response = userClient.getById(createBy);
            if (response.getCode() == 1) {
                vo.setUploadUser(response.getData().getNickname());
            }
            return vo;
        }).collect(Collectors.toList());

        return Optional.of(fileVOList).orElse(new ArrayList<>());
    }
}




